package jm.mq.bootconfirm.conf;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @Description 发布确认绑定配置类
 * @date 2022/3/13 9:24
 */
@Configuration
public class BingConf {

    public static final String CONFIRM_EXCHANGE = "confirm_exchange";

    public static final String CONFIRM_QUEUE = "confirm_queue";

    public static final String CONFIRM_ROTING_KEY = "confirm_routing_key";

    public static final String BACKUP_EXCHANGE = "backup_exchange";

    public static final String BACKUP_QUEUE = "backup_queue";

    public static final String WARNING_QUEUE = "warning_queue";


    @Bean("c_ex")
    public DirectExchange confirmChange(){
        return ExchangeBuilder.directExchange(CONFIRM_EXCHANGE).
                withArgument("alternate-exchange",BACKUP_EXCHANGE)
                .build();
    }

    @Bean("b_ex")
    public FanoutExchange backupChange(){
        return new FanoutExchange(BACKUP_EXCHANGE);
    }

    @Bean("c_qu")
    public Queue confirmQueue(){
        return QueueBuilder.durable(CONFIRM_QUEUE).build();
    }

    @Bean("b_qu")
    public Queue backupQueue(){
        return QueueBuilder.durable(BACKUP_QUEUE).build();
    }

    @Bean("w_qu")
    public Queue warningQueue(){
        return QueueBuilder.durable(WARNING_QUEUE).build();
    }

    @Bean()
    public Binding bindingQueueAndExchange(@Qualifier("c_ex") DirectExchange exchange,
                                           @Qualifier("c_qu") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with(CONFIRM_ROTING_KEY);
    }

    @Bean()
    public Binding bindingBackupQueueAndBackupExchange(@Qualifier("b_ex") FanoutExchange exchange,
                                                 @Qualifier("b_qu") Queue backQueue){
        return BindingBuilder.bind(backQueue).to(exchange);
    }

    @Bean()
    public Binding bindingWarningQueueAndBackupExchange(@Qualifier("b_ex") FanoutExchange exchange,
                                                 @Qualifier("w_qu") Queue warningQueue){
        return BindingBuilder.bind(warningQueue).to(exchange);
    }
}

